<?php

namespace App\Admin\Controllers;

use App\Models\Navbar;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Encore\Admin\Tree;

class NavbarController extends AdminController
{
    protected function title()
    {
        return '导航';
    }

    public function index(Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description['index'] ?? trans('admin.list'))
            ->body($this->tree());
    }

    protected function tree()
    {
        return Navbar::tree(function (Tree $tree) {
            $tree->branch(function ($branch) {
                $isShow = ['隐藏', '显示'];
                return "{$branch['id']} - {$branch['name']} -- {$isShow[$branch['is_show']]}";
            });
        });
    }

    protected function form()
    {
        $form = new Form(new Navbar);

        $form->select('pid', '父栏目')->options(Navbar::selectOptions());
        $form->text('name', '栏目名称')->rules('required');
        $form->text('url', '栏目地址');
        $status = [
            'on'  => ['value' => 1, 'text' => '显示', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '隐藏', 'color' => 'danger'],
        ];
        $form->radio('target', '打开方式')->options(['0' => '新窗口', '1'=> '本窗口'])->default('0');
        $form->switch('is_show', '是否显示')->states($status);

        return $form;
    }
}
